﻿package
{
	import com.onlyMe.config.ConfigNode;
	import com.onlyMe.globals.config;
	import com.onlyMe.view.world.IsleView;
	import com.onlyMe.world.Isle;
	import com.onlyMe.world.IsleEvent;
	import com.qb9.flashlib.color.Color;
	import com.qb9.flashlib.geom.Anchor;
	import com.qb9.flashlib.prototyping.shapes.BasicShapeConfig;
	import com.qb9.flashlib.prototyping.shapes.Square;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * @author ariel
	 */
	
	[SWF(width=700, height=500, backgroundColor=0x000000, frameRate=25)]
	public class Main extends Sprite
	{
		private static const CONFIG_URI:String = 'config/config.xml';
		
		private var isle:Isle;
		private var isleView:IsleView;
		
		public function Main():void
		{
			init();
		}
		
		private function init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var loader:URLLoader = new URLLoader(new URLRequest(CONFIG_URI));
			loader.addEventListener(Event.COMPLETE, parseConfig);
		}
		
		private function parseConfig(e:Event):void
		{
			var xml:XML = new XML(e.target.data);
			
			config = new ConfigNode().decode(xml);
			initIsle();
		}
		
		private function initIsle():void
		{
			isle = new Isle();
			isleView = new IsleView(isle);
			addChild(isleView)
			
			isle.addEventListener(IsleEvent.READY, addObjects);
		}
		
		private function addObjects(e:Event):void
		{
			var ba:ByteArray = new ByteArray();
			// Avatar
			ba.writeShort(0);
			ba.writeByte(12);
			ba.writeByte(9);
			
			ba.position = 0;
			isle.readExternal(ba);
		}
	}
	
}
